package com.hurst.autotap;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHidDevice;
import android.bluetooth.BluetoothHidDeviceAppSdpSettings;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.text.TextUtils;

import java.util.concurrent.Executors;

public class HidUitls {
    public static String SelectedDeviceMac = "";
    public static boolean _connected = false;
    public static boolean IsRegisted = false;
    public static CommonInterface.ConnectionStateChangeListener connectionStateChangeListener;

    static BluetoothAdapter mBluetoothAdapter;
    static BluetoothProfile bluetoothProfile;
    static BluetoothDevice BtDevice;
    static BluetoothHidDevice HidDevice;

    public static void RegistApp(Context context){
        if(IsRegisted) { }
        else{ BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, mProfileServiceListener, BluetoothProfile.HID_DEVICE); }
    }

    public static boolean Pair(String deviceAddress){
        if(BluetoothAdapter.checkBluetoothAddress(deviceAddress)){
            try {
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if(BtDevice == null){
                    BtDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress);
                }
                if(BtDevice.getBondState() == BluetoothDevice.BOND_NONE){
                    BtDevice.createBond();
                    return false;
                }else if(BtDevice.getBondState() == BluetoothDevice.BOND_BONDED){
                    return true;
                }else if(BtDevice.getBondState() == BluetoothDevice.BOND_BONDING){
                    return false;
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        return false;
    }

    public static boolean IsConnected() {
        return HidUitls._connected;
    }

    private static void IsConnected(boolean _connected) {
        HidUitls._connected = _connected;
    }
    public static boolean Connect(String deviceAddress){
        if(TextUtils.isEmpty(deviceAddress)){return false;}
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if(BtDevice == null){
            BtDevice = mBluetoothAdapter.getRemoteDevice(deviceAddress);
        }
        boolean ret = HidDevice.connect(BtDevice);
        HidConsts.BtDevice = BtDevice;
        HidConsts.HidDevice = HidDevice;
        return ret;
    }
    public static boolean Connect(BluetoothDevice device){
        boolean ret = HidDevice.connect(device);
        HidConsts.BtDevice = device;
        HidConsts.HidDevice = HidDevice;
        return ret;
    }

    public static void ReConnect(final Activity context){
        try {
            if (HidUitls.HidDevice != null) {
                if (HidUitls.BtDevice == null) {
                    HidUitls.BtDevice = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(HidUitls.SelectedDeviceMac);
                }
                int state = HidUitls.HidDevice.getConnectionState(HidUitls.BtDevice);
                if (state == BluetoothProfile.STATE_DISCONNECTED) {
                    if (TextUtils.isEmpty(HidUitls.SelectedDeviceMac)) {
                    } else {
                        if (HidUitls.Pair(HidUitls.SelectedDeviceMac)) {
                            HidUitls.RegistApp(context.getApplicationContext());
                            UtilCls.DelayTask(new Runnable() {
                                @Override
                                public void run() {
                                    context.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            HidUitls.Connect(HidUitls.SelectedDeviceMac);
                                        }
                                    });
                                }
                            }, 500, true);
                        }
                    }
                }
            }
        }catch (Exception ex){ }
    }

    public static BluetoothProfile.ServiceListener mProfileServiceListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceDisconnected(int profile) { }
        @SuppressLint("NewApi") @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {
            bluetoothProfile = proxy;
            if (profile == BluetoothProfile.HID_DEVICE) {
                HidDevice = (BluetoothHidDevice) proxy;
                HidConsts.HidDevice = HidDevice;
                BluetoothHidDeviceAppSdpSettings sdp = new BluetoothHidDeviceAppSdpSettings(HidConsts.NAME, HidConsts.DESCRIPTION, HidConsts.PROVIDER
                        , BluetoothHidDevice.SUBCLASS2_DIGITIZER_TABLET, HidConsts.Descriptor);
                HidDevice.registerApp(sdp, null, null, Executors.newCachedThreadPool(), mCallback);
            }
        }
    };
    public static final BluetoothHidDevice.Callback mCallback = new BluetoothHidDevice.Callback() {
        @Override
        public void onAppStatusChanged(BluetoothDevice pluggedDevice, boolean registered) {
            IsRegisted = registered;
        }
        @Override
        public void onConnectionStateChanged(BluetoothDevice device, int state) {
            if(state == BluetoothProfile.STATE_DISCONNECTED){
                HidUitls.IsConnected(false);
                if(connectionStateChangeListener != null){
                    connectionStateChangeListener.onDisConnected();
                }
            }else if(state == BluetoothProfile.STATE_CONNECTED){
                HidUitls.IsConnected(true);
                if(connectionStateChangeListener != null){
                    connectionStateChangeListener.onConnected();
                }
            }else if(state == BluetoothProfile.STATE_CONNECTING){
                if(connectionStateChangeListener != null){
                    connectionStateChangeListener.onConnecting();
                }
            }
        }
    };
}
